package com.ycsj.goldmedalnewconcept.master.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.master.adapter.TeacherManagerRvAdapter;
import com.ycsj.goldmedalnewconcept.master.bean.TeacherManagerInfo;
import com.ycsj.goldmedalnewconcept.parent.activity.ParentsEvaluateTeacherActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 教师管理
 * Created by Administrator on 2018/7/16 0016.
 */

public class MasterManagerTeachersActivity extends BaseActivity implements SrfManager.OnPullRefreshListener, View.OnClickListener {

    private ImageView ivBack;
    private RecyclerView rv;
    private SmartRefreshLayout srf;
    private RelativeLayout rlLoadingView;
    //内容布局
    private RelativeLayout rlConent;

    //空视图
    private View emptyView;

    //数据集
    private List<TeacherManagerInfo.ListBean> oldDatas;

    //适配器
    private TeacherManagerRvAdapter adapter;

    //学校名称
    public static String schoolName;

    //学校名称
    private TextView tvSchoolName;

    //标题
    private TextView tv_st_title;

    //跳转的Type 1为教师管理跳转,2家长评价,3教师备课
    private String type;

    //记录schoolid
    private String schoolid;

    /**
     * 跳转到当前页面的方法
     */
    public static void goActivity(Context context, String type) {
        Intent it = new Intent(context, MasterManagerTeachersActivity.class);
        it.putExtra("type", type);
        context.startActivity(it);
    }


    @Override
    protected int getContentLayout() {
        return R.layout.activity_master_manager_teachers;
    }

    @Override
    protected void init() {

        initStateBar();

        //获取跳转的类型
        initIntent();

        //初始化视图
        initView();

        //初始化下拉刷新控件
        initSrf();

        //设置数据
        initRv();

        //请求数据
        http(BqaManager.NORML);
    }


    /**
     * 获取跳转的类型
     */
    private void initIntent() {
        type = getIntent().getStringExtra("type");
    }


    /**
     * 初始化视图
     */
    private void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        srf = findViewById(R.id.srf);
        tvSchoolName = findViewById(R.id.tv_school_name);
        rlLoadingView = findViewById(R.id.rl_loading);
        rlLoadingView.setVisibility(View.VISIBLE);
        rlConent = findViewById(R.id.rl_content);
        rlConent.setVisibility(View.GONE);
        tv_st_title = findViewById(R.id.tv_title);

        //根据跳转到类型设置标题 1为教师管理跳转,2家长评价,3教师备课
        if ("1".equals(type)) {
            tv_st_title.setText("教师管理");
        } else if ("2".equals(type)) {
            tv_st_title.setText("家长评价");
        } else if ("3".equals(type)) {
            tv_st_title.setText("教师备课");
        }

    }

    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 设置列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        adapter = new TeacherManagerRvAdapter(this, R.layout.master_rv_manager_teacher, oldDatas);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if ("1".equals(type)) {
                    //教师管理跳转到教师详情界面
                    Intent intent = new Intent(MasterManagerTeachersActivity.this,
                            MasterTeacherInfoActivity.class);
                    intent.putExtra("data", oldDatas.get(position));
                    startActivity(intent);
                } else if ("2".equals(type)) {
                    //校长端查看家长评价,先选择老师界面,携带老师的账号进行跳转
                    Intent intent = new Intent(MasterManagerTeachersActivity.this,
                            ParentsEvaluateTeacherActivity.class);
                    intent.putExtra("teacherid", oldDatas.get(position).getUserid());
                    startActivity(intent);
                } else if ("3".equals(type)) {
                    //校长端查看教师备课,先选择老师界面,携带老师的账号进行跳转
                    MasterChooseUnitActivity.goActivity(MasterManagerTeachersActivity.this, oldDatas.get(position).getUserid(), schoolid);
                }
            }
        });
    }


    /**
     * 请求服务器数据的方法
     *
     * @param method
     */
    private void http(final int method) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "15");
        map.put("c", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("platform", "2");
        HttpUtils.post(HttpAddress.GETTEACHERS, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLoadingView != null && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }
                ToastUtils.showShort(BaseApplication.ERROR);
            }

            @Override
            public void onResponse(String response, int id) {
                if (null != rlLoadingView && rlLoadingView.getVisibility() != View.GONE) {
                    rlLoadingView.setVisibility(View.GONE);
                }

                if (null != rlConent && rlConent.getVisibility() != View.VISIBLE) {
                    rlConent.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(response)) {
                    TeacherManagerInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, TeacherManagerInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("1".equals(info.getState())) {

                            //保存schoolid
                            schoolid = info.getSchool();

                            //返回成功
                            List<TeacherManagerInfo.ListBean> newDatas = info.getList();

                            //学校名称
                            schoolName = info.getSchoolName();

                            if (method == BqaManager.NORML || method == BqaManager.PULLREFRESH) {
                                //设置学校名称
                                if (!TextUtils.isEmpty(schoolName)) {
                                    tvSchoolName.setText(schoolName);
                                } else {
                                    tvSchoolName.setText("暂无");
                                }

                            }

                            if (newDatas != null) {
                                if (method == BqaManager.NORML) {
                                    oldDatas.clear();
                                    oldDatas.addAll(newDatas);
                                    GridLayoutManager manager = new GridLayoutManager(MasterManagerTeachersActivity.this, 4);
                                    BqaManager.setRv(emptyView, manager, MasterManagerTeachersActivity.this, adapter, rv, null);
                                } else {
                                    //更新数据
                                    BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                                    adapter.setEnableLoadMore(false);
                                }
                            }

                        } else {
                            ToastUtils.showShort(Constants.ERRORSTATE + info.getState());
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }

            }
        });
    }

    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }
}
